//
//  FQDHeadlineContentView.m
//  FakeQDaily
//
//  Created by mewxiv on 9/12/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDHeadlineContentView.h"

#import "Masonry.h"

@interface FQDHeadlineContentView ()

// 内容文本
@property (nonatomic, strong) UILabel *contentLabel;
// 小黄点
@property (nonatomic, strong) UIImageView *dotIcon;
// 分割线
@property (nonatomic, strong) UIView *separator;

@end

@implementation FQDHeadlineContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self updateContentLabelHeight];
}

- (void)prepareUI
{
    self.opaque = YES;
    UIImage *dotImage = [UIImage imageNamed:@"composite_point"];
    _dotIcon = [[UIImageView alloc] initWithImage:dotImage];
    [self addSubview:_dotIcon];
    
    WeakSelf;
    [_dotIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(15);
        make.centerY.equalTo(weakSelf);
        make.size.equalTo(@7);
    }];
    
    UIView *separator = [[UIView alloc] init];
    separator.backgroundColor = RGB(222, 222, 222);
    [self addSubview:separator];
    self.separator = separator;
    
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(37);
        make.right.equalTo(weakSelf).offset(-15);
        make.bottom.equalTo(weakSelf);
        make.height.equalTo(@0.5);
    }];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = FQDRegularFontWithSize(15.0f);
    _contentLabel.textColor = RGB(42, 42, 42);
    _contentLabel.numberOfLines = 0;
    [self addSubview:_contentLabel];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(8);
        make.left.and.right.equalTo(weakSelf.separator);
        make.bottom.equalTo(weakSelf.separator).offset(-8);
    }];

}

- (void)updateContentLabelHeight
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 5.0f;
    NSDictionary *attrs = @{
        NSFontAttributeName : FQDRegularFontWithSize(15.0f),
        NSParagraphStyleAttributeName : paraStyle,
    };
    
    // preferredMaxLayoutWidth系统自动设置
    CGFloat textLayoutWidth = self.contentLabel.preferredMaxLayoutWidth;
    CGSize textSize = [self.contentLabel.text boundingRectWithSize:CGSizeMake(textLayoutWidth, CGFLOAT_MAX)
                       options:NSStringDrawingUsesLineFragmentOrigin
                       attributes:attrs context:nil].size;
    CGFloat textHeight = ceil(textSize.height);
    
    WeakSelf;
    [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(8);
        make.left.and.right.equalTo(weakSelf.separator);
        make.height.equalTo(@(textHeight));
        make.bottom.equalTo(weakSelf.separator).offset(-8);
    }];
}

- (void)setContent:(NSString *)content
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 5.0f;
    NSDictionary *attrs = @{
        NSFontAttributeName : FQDRegularFontWithSize(15.0f),
        NSParagraphStyleAttributeName : paraStyle,
    };
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:content
                                                                     attributes:attrs];
    self.contentLabel.attributedText = attrString;
    
}

- (NSString *)content
{
    return self.contentLabel.attributedText.string;
}

@end
